﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Security.Cryptography;
using System.IO;
using System.Text;
using Portal;
using Workflow.Entity;
using Workflow.Logic;
using System.Collections;
using Workflow.Interface;
using MyLib;

namespace PortalWebService
{
    /// <summary>
    /// Blank1 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]

    public class TaskService : System.Web.Services.WebService
    {

        ITask _task = ServiceContainer.Instance().GetService<ITask>();
        [WebMethod(Description = "获取有权限发起的流程")]
        public List<ProcessEntity> GetInitProcessList(string userName)
        {
            userName = userName.Replace("\\","/");
            return _task.GetInitProcessList(userName);
        }

        [WebMethod(Description = "获取待办任务")]
        public List<TaskEntity> GetTaskList(string userName)
        {
            userName = userName.Replace("\\", "/");
            Hashtable table = new Hashtable();
            table.Add("STARTTIME", DateTime.Now.AddDays(-90));
            table.Add("ENDTIME", DateTime.Now.AddDays(90));
            table.Add("ASSIGNEDTOUSER", "'" + userName + "'");

            return _task.GetMyTask("", table, " a.StartTime", 0, 999);
        }

        [WebMethod(Description = "提交任务")]
        public string SubmitTask(string userName, string taskId, string summary, string vars)
        {

            userName = userName.Replace("\\", "/");
            int incident=0;
            Hashtable table = new Hashtable();
            if (!string.IsNullOrEmpty(vars))
            {
                string[] sz = vars.Split(',');
                for (int i = 0; i < sz.Length; i++)
                {
                    table.Add(sz[i].Split('|')[0], sz[i].Split('|')[1]);
                }
            }
            return _task.SubmitTask(userName, taskId, summary, table, true, ref incident);
        }

        [WebMethod(Description = "退回任务")]
        public string ReturnTask(string userName, string taskId, string reason, string summary, string vars)
        {

            userName = userName.Replace("\\", "/");
            Hashtable table = new Hashtable();
            if (!string.IsNullOrEmpty(vars))
            {
                string[] sz = vars.Split(',');
                for (int i = 0; i < sz.Length; i++)
                {
                    table.Add(sz[i].Split('|')[0], sz[i].Split('|')[1]);
                }
            }
            return _task.ReturnTask(userName, taskId, reason, summary, table, true);
        }

        [WebMethod(Description = "拒绝任务")]
        public string RejectTask(string userName, string taskId, string reason, string vars)
        {

            userName = userName.Replace("\\", "/");
            Hashtable table = new Hashtable();
            if (!string.IsNullOrEmpty(vars))
            {
                string[] sz = vars.Split(',');
                for (int i = 0; i < sz.Length; i++)
                {
                    table.Add(sz[i].Split('|')[0], sz[i].Split('|')[1]);
                }
            }
            return _task.RejectTask(userName, taskId, reason, table, true);
        }
    }
}
